﻿#include "selectmodulepage.h"
#include "ui_selectmodulepage.h"
#include "global_funcs.h"
#include <QStringListModel>
#include <QTimer>
#include <QMessageBox>
#include <QDebug>

SelectModulePage::SelectModulePage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::SelectModulePage)
{
    ui->setupUi(this);
    stringListModel = new QStringListModel(this);
    ui->listView->setModel(stringListModel);
    connect(ui->listView->selectionModel(), &QItemSelectionModel::currentRowChanged,
            this, &SelectModulePage::currentRowChanged);
}

SelectModulePage::~SelectModulePage()
{
    delete ui;
}

QString SelectModulePage::getSelectedModuleName()
{
    return stringListModel->data(ui->listView->currentIndex()).toString();
}

void SelectModulePage::initializePage()
{
    QStringList moduleNames;
    if (!enumModule(moduleNames)) {
        qCritical() << "enumModule failed!";
        QMessageBox::critical(this, windowTitle(), QStringLiteral("enumModule failed!"));
        QTimer::singleShot(0, this->wizard(), SLOT(reject()));
        return;
    }

    stringListModel->setStringList(moduleNames);
    ui->listView->setCurrentIndex(stringListModel->index(-1));
}

bool SelectModulePage::isComplete() const
{
    return ui->listView->currentIndex().isValid();
}

QSize SelectModulePage::sizeHint() const
{
    return QSize(800, 600);
}

void SelectModulePage::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
    emit completeChanged();
}

void SelectModulePage::on_listView_doubleClicked(const QModelIndex &index)
{
    if (index.isValid())
        wizard()->next();
}

